package zucc.edu.cn.chengchengparttime.Workdetails;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import zucc.edu.cn.chengchengparttime.MainActivity;
import zucc.edu.cn.chengchengparttime.My.MyAppSetting;
import zucc.edu.cn.chengchengparttime.R;
import zucc.edu.cn.chengchengparttime.Text;
import zucc.edu.cn.chengchengparttime.Work;
import zucc.edu.cn.chengchengparttime.dbHomeHelper;

public class WorkdetailsActivity extends AppCompatActivity {
    private static final String DEBUG_TAG="Chengcheng Log";
    private SQLiteDatabase mDatabase;// 数据库实例
    dbHomeHelper homeHelper;

    private Context context;
    RecyclerView rv; //RecyclerView布局
    WorkdetailsAdapter workdetailsAdapter; //适配器
    List<Work> data=new ArrayList<>(); //数据列表
    private ImageView goback;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_workdetails_main);
        context = this.getBaseContext();

        //以下三行获取当前选中的item
        Intent intent=getIntent();
        long id=intent.getLongExtra("item",0);
        Log.d("id",id+"");//测试

        mDatabase=SQLiteDatabase.openDatabase("/data/user/0/zucc.edu.cn.chengchengparttime/databases/Chengcheng.db",null,SQLiteDatabase.OPEN_READWRITE);//获取数据库
        Log.d("Chengcheng.db","获取数据库"+mDatabase);

        try (
            Cursor c = mDatabase.query("tbl_work", null, "WorkID=?", new String[]{id + ""}, null, null, null)) {
            c.moveToFirst();//游标移到第一条记录

            // 打印记录
            String[] x3 = new String[30];
            while (!c.isAfterLast()) {
                String rowResults = "||";
                for (int i = 0; i < c.getColumnCount(); i++) {
                    rowResults = rowResults.concat(c.getString(i) + "||");
                    x3[i] = c.getString(i);
                }
                Work work=new Work(Long.parseLong(x3[0]),x3[1],x3[2],x3[3],x3[4],x3[5],Double.parseDouble(x3[6]),x3[7],x3[8],x3[9],Integer.parseInt(x3[10]));
                data.add(work);
                data.add(work);
                Log.d("work", work.toString());//测试

                c.moveToNext();
            }
            c.close();//关闭游标工厂
        }

        //配置布局管理器、分割线、适配器
        rv = (RecyclerView) findViewById(R.id.rv);
        //第一步：设置布局管理器
        rv.setLayoutManager(new LinearLayoutManager(this));
        //第三步：设置适配器
        workdetailsAdapter=new WorkdetailsAdapter(this,data,id,mDatabase);
        rv.setAdapter(workdetailsAdapter);

        goback=findViewById(R.id.goback);
        goback.setOnClickListener(view -> {
            Toast.makeText(this.context, "点击了返回按钮" , Toast.LENGTH_SHORT).show();
            //跳转到首页（首先要跳转到MainActivity，然后跳转到HomeFragment）
            Intent i=new Intent();
            i.setClass(WorkdetailsActivity.this, MainActivity.class);
            //一定要指定是第几个pager，这里填写1
            i.putExtra("id",1);
            startActivity(i);
        });
    }
}
